package com.erp.activityIKP

import android.content.Context
import android.util.Log
import com.erp.activityKView.XIaoZuKView
import com.erp.bean.BenZuKBean
import com.erp.bean.XiaoZuKBean
import com.erp.Interface.OnUserDataInterface
import com.erp.activityKP.XiaoZuKP
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import org.json.JSONException
import org.json.JSONObject
import java.util.*

/**
 * Created by Administrator on 2017/11/9/009.
 * 作者：刘洋
 */
class IXiaoZuKP(val xiaoZuKView: XIaoZuKView ,val context : Context) : XiaoZuKP{

    var page=0
    val pagesize=20
    var name=""
    var gonghao=""
    var zuhao=""
    override fun getXiaoZuShuJu() {
        page=0
        Httputil.getXiaoZu(page,pagesize,name,gonghao,zuhao,context,object : OnUserDataInterface{
            override fun onUserData(data: String) {
                Log.d("TAAAG","data="+data)
                var xiaoZuKBeanList = jsonshuju(data)
                xiaoZuKView.xiaoZuShuJuOk(xiaoZuKBeanList)
            }

            override fun onNot(throwable: String) {
                Log.d("TAAAG","error="+Myutil.decodeUnicode(throwable))
                xiaoZuKView.xiaoZShuJuNo(throwable)
            }

        })
    }

    override fun addXiaoZuShuJu() {
        page+=1
        Httputil.getXiaoZu(page,pagesize,name,gonghao,zuhao,context,object : OnUserDataInterface{
            override fun onUserData(data: String) {
                Log.d("TAAAG","data="+data)
                var xiaoZuKBeanList = jsonshuju(data)
                xiaoZuKView.addXiaoZuShuJuOk(xiaoZuKBeanList)
            }

            override fun onNot(throwable: String) {
                Log.d("TAAAG","error="+throwable)
                xiaoZuKView.addxiaoZShuJuNo(throwable)
            }

        })
    }
    override fun setXiaoZU(xiaozu: XiaoZuKBean) {
        var s=StringBuffer("\"")
        for(benzu in xiaozu.xiaoZuBean){
            s.append(benzu.work_number+" ")
            s.append(benzu.coefficient+"\\n")
        }
        s.append("\"")
        Httputil.setZuYuan(context,s.toString(),object : OnUserDataInterface{
            override fun onUserData(data: String) {
                Myutil.longlogshow("TAAAG","data="+Myutil.decodeUnicode(data))
                xiaoZuKView.XuanZeHttpXiaoZuOk(data)
            }

            override fun onNot(throwable: String) {
                Log.d("TAAAG","throwable="+throwable)
                xiaoZuKView.XuanZuHttoXiaozuNo(throwable)
            }

        })
    }
    fun jsonshuju(data : String) : MutableList<XiaoZuKBean>{
        var xiaoZuKBeanList = ArrayList<XiaoZuKBean>()
        try {
            var jo= JSONObject(data)
            val it = jo.keys()
            while(it.hasNext()){
                var xiaoZuKBean=XiaoZuKBean()
                var key =it.next()
                var jo1=jo.getJSONObject(key)
                val it1=jo1.keys()
                while (it1.hasNext()){
                    var key1=it1.next()
                    val jo2=jo1.getJSONObject(key1)
                    var benZuBean=BenZuKBean()
                    benZuBean.wu_id= JsonUtil.getvolue(jo2,"wu_id")
                    benZuBean.coefficient= JsonUtil.getvolue(jo2,"coefficient")
                    benZuBean.coefficient_total= JsonUtil.getvolue(jo2,"coefficient_total")
                    benZuBean.wu_name= JsonUtil.getvolue(jo2,"wu_name")
                    benZuBean.work_number= JsonUtil.getvolue(jo2,"work_number")
                    benZuBean.gangwei_name= JsonUtil.getvolue(jo2,"gangwei_name")
                    xiaoZuKBean.gnum=JsonUtil.getvolue(jo2,"gnum")
                    xiaoZuKBean.xiaoZuBean.add(benZuBean)
                }
                xiaoZuKBeanList.add(xiaoZuKBean)
            }
        }catch (e : JSONException){
            e.printStackTrace()
            Log.d("TAAAG","e="+e.toString())
        }
        return xiaoZuKBeanList
    }
    override fun setSuoSuo(name: String, gonghao: String, zuhao: String) {
        this.name=name
        this.gonghao=gonghao
        this.zuhao=zuhao
    }

    override fun cleanSouSuo() {
        name=""
        gonghao=""
        zuhao=""
    }

}